package com.lk.v1;

import com.lk.base.mysql.domain.TblDevices;
import com.lk.base.mysql.domain.TblSysset;
import com.lk.base.utils.ResultUtil;
import com.lk.device.dto.DeviceDTO;
import com.lk.device.service.DevicesService;
import com.lk.device.service.SyssetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Controller
@RequestMapping("/hub/v1")
public class SysSetController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SyssetService syssetService;

    @Autowired
    private DevicesService devicesService;


    @PostMapping("/sysset/one")
    @ResponseBody
    public ResponseEntity one(TblSysset dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        List<TblSysset> tblSyssets = syssetService.selectAll();
        if(tblSyssets!= null && tblSyssets.size()>0){
            return new ResponseEntity(tblSyssets.get(0), HttpStatus.OK);
        }
        return new ResponseEntity(null, HttpStatus.OK);
    }



    @PostMapping("/sysset/save")
    @ResponseBody
    public ResponseEntity save(TblSysset dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return new ResponseEntity(syssetService.save(dto), HttpStatus.OK);
    }

    @PostMapping("/device/type")
    @ResponseBody
    public ResponseEntity deviceType(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        dto.setDeviceType(dto.getDeviceType());
        List<TblDevices> list = (List<TblDevices>)devicesService.selectByType(dto);
        return new ResponseEntity(list, HttpStatus.OK);
    }

    @PostMapping("/device/all")
    @ResponseBody
    public ResponseEntity all(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        List<TblDevices> list = (List<TblDevices>)devicesService.selectAll(dto);
        return new ResponseEntity(list, HttpStatus.OK);
    }

    @PostMapping("/device/saveDevice")
    @ResponseBody
    public ResponseEntity saveDevice(TblDevices dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {


        devicesService.save(dto);
        return new ResponseEntity(ResultUtil.Ok(dto.getId() != null ? "修改成功":"新增成功"), HttpStatus.OK);
    }

    @PostMapping("/device/deleteDevice")
    @ResponseBody
    public ResponseEntity deleteDevice(DeviceDTO dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {


        devicesService.deleteDevice(dto);
        return new ResponseEntity(ResultUtil.Ok("删除成功"), HttpStatus.OK);
    }

    @PostMapping("/device/saveDeviceStatus")
    @ResponseBody
    public ResponseEntity saveDeviceStatus(TblDevices dto) throws UnsupportedEncodingException, NoSuchAlgorithmException {


        devicesService.saveDeviceStatus(dto);
        return new ResponseEntity(ResultUtil.Ok(dto.getId() != null ? "发送经纬度成功":"发送经纬度失败"), HttpStatus.OK);
    }
}
